package org.i0o.utilplus.populator.converters;

import org.i0o.utilplus.populator.exception.ConversionException;

/**
 * Boolean Converter
 * 
 * @author <a href="mailto:781131@gmail.com">HTF</a>
 * @version 2010-5-28
 */
public final class BooleanConverter extends BaseConverter {

	private String[] trueStrs = {
			"true", "yes", "y", "on", "1"
	};

	private String[] falseStrs = {
			"false", "no", "n", "off", "0"
	};

	/*
	 * (non-Javadoc)
	 * @seecom.citinet.webplus.util.populator.converters.BaseConverter#
	 * convertToType(java.lang.Object, java.lang.Class)
	 */
	@Override
	protected Object convertToType(Object value, Class<?> targetType,Object... configs) {
		value = value.toString().toLowerCase();

		// true
		for (String trueTmp : trueStrs) {
			if (trueTmp.equals(value)) {
				return Boolean.TRUE;
			}
		}

		// false
		for (String flaseTmp : falseStrs) {
			if (flaseTmp.equals(value)) {
				return Boolean.FALSE;
			}
		}

		throw new ConversionException("Can't convert value " + value + " to a Boolean.");
	}

	@Override
	protected Class<?> getDefaultType() {
		return Boolean.class;
	}

}
